import java.util.HashMap;
import java.util.Map;

public class Leetcode1072 {
    public static int maxEqualRowsAfterFlips(int[][] matrix) {
        int res = 0;
        Map<String, Integer> count = new HashMap<>();
        for (int[] ints : matrix) {
            StringBuilder s0 = new StringBuilder();
            StringBuilder s1 = new StringBuilder();
            for (int columnIndex = 0; columnIndex < ints.length; columnIndex++) {
                (ints[columnIndex] == 0 ? s0 : s1).append(columnIndex).append("-");
            }

            String s0Str = s0.toString();
            String s1Str = s1.toString();
            if (!s0Str.isBlank()) {
                int value = count.getOrDefault(s0Str, 0) + 1;
                res = Math.max(value, res);
                count.put(s0Str, value);
            }
            if (!s1Str.isBlank()) {
                int value = count.getOrDefault(s1Str, 0) + 1;
                res = Math.max(value, res);
                count.put(s1Str, value);
            }
        }

        return res;
    }

    public static void main(String[] args) {
        System.out.println(maxEqualRowsAfterFlips1(new int[][]{
                {0, 1},
                {1, 1},
        }));

        System.out.println(maxEqualRowsAfterFlips1(new int[][]{
                {0, 1},
                {1, 0},
        }));

        System.out.println(maxEqualRowsAfterFlips1(new int[][]{{0, 0, 0}, {0, 0, 1}, {1, 1, 0}}));
    }

    public static int maxEqualRowsAfterFlips1(int[][] matrix) {
        int res = 0;
        Tree root = new Tree();
        for (int[] ints : matrix) {
            Tree tree = root;
            for (int m : ints) {
                if (m == ints[0]) {
                    if (tree.left == null) {
                        tree.left = new Tree();
                    }

                    tree = tree.left;
                } else {
                    if (tree.right == null) {
                        tree.right = new Tree();
                    }

                    tree = tree.right;
                }
            }

            tree.val++;
            if (tree.val > res) {
                res = tree.val;
            }
        }

        return res;
    }

    static class Tree {
        Tree left;
        Tree right;
        int val;
    }

}

